﻿Imports System.Windows.Forms
Imports System.Text
Imports System.Drawing


Public Class FrmEncoders


    Private EncodingClass As New ClsEncoding


    Private Sub FrmEncoders_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        LoadListView()

    End Sub


    Private Sub LoadListView()

        Dim EncInfo As EncodingInfo

        With Me.lvEncoders

            .Columns.Add("Code Page", 50, HorizontalAlignment.Left)
            .Columns.Add("Name", 100, HorizontalAlignment.Left)
            .Columns.Add("Display Name", 300, HorizontalAlignment.Left)

        End With

        Dim Enc As Encoding = Encoding.GetEncoding(0)
        PopulateListView(0, "Default", Enc.CodePage & " - " & Enc.EncodingName)

        For Each EncInfo In Encoding.GetEncodings()

            PopulateListView(EncInfo.CodePage, EncInfo.Name, EncInfo.DisplayName)

        Next EncInfo

    End Sub


    Private Sub PopulateListView(ByRef codePage As Integer, ByRef encName As String, _
                                 ByRef encDisplayName As String)

        Dim lvItem As ListViewItem
        Dim EncItem(3) As String

        EncItem(0) = codePage
        EncItem(1) = encName
        EncItem(2) = encDisplayName

        lvItem = New ListViewItem(EncItem)

        If EncodingClass.EncoderCodePage = codePage Then
            lvItem.BackColor = Color.Silver
        Else
            lvItem.BackColor = Color.White
        End If

        Select Case codePage
            Case 0, 1200, 1201, 1252, 12000, 12001, 20127, 65000, 65001
                lvItem.ForeColor = Color.Blue
            Case Else
                lvItem.ForeColor = Color.Black
        End Select

        lvEncoders.Items.Add(lvItem)

    End Sub


#Region "BUTTON Events"


    Private Sub BtnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                            Handles BtnGo.Click

        SelectEncoder()
        Me.DialogResult = DialogResult.OK
        Me.Close()

    End Sub


    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                               Handles BtnCancel.Click


        Me.DialogResult = DialogResult.Cancel
        Me.Close()

    End Sub


    Private Sub BtnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                               Handles BtnApply.Click

        SelectEncoder()
        Me.lvEncoders.Clear()
        LoadListView()

    End Sub


#End Region


    Private Sub SelectEncoder()

        Try

            Dim EncClass As New ClsEncoding()
            EncClass.EncoderCodePage = Me.lvEncoders.SelectedItems(0).Text()

        Catch ex As ArgumentOutOfRangeException

            MessageBox.Show("You need to select something!", MessageBoxTitle, _
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End Try

    End Sub


    Private Sub LinkRebarFrameAbout_LinkClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)

        'Dim AboutForm As New RebarAboutForm
        'AboutForm.Show()

    End Sub

End Class